package com.ruoyi.web.controller.xxc;


import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.news.domain.XcxBlack;
import com.ruoyi.news.domain.XcxGoodsOrder;
import com.ruoyi.news.domain.XcxOrderMain;
import com.ruoyi.news.domain.XcxOrderState;
import com.ruoyi.news.service.IXcxOrderLogService;
import com.ruoyi.news.service.IXcxOrderMainService;
import com.ruoyi.news.service.IXcxOrderStateService;
import com.ruoyi.web.util.OrderPo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;

@Api(tags = "订单流程")
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IXcxOrderMainService xcxOrderMainService;

    @Autowired
    private IXcxOrderStateService xcxOrderStateService;

    @Autowired
    private IXcxOrderLogService xcxOrderLogService;

    //全局费率
    private final static String raid = "0.02";


    //接手订单
    @PostMapping("/takeOrder")
    public AjaxResult takeOrder(@RequestBody XcxOrderMain xcxOrderMain, @AuthenticationPrincipal UserDetails user)
    {
        LoginUser loginUser =  (LoginUser)user;
        SysUser sysUser = loginUser.getUser();
        xcxOrderMain.setTakeUser(sysUser.getUserId()+"");

        return AjaxResult.updateData(xcxOrderMainService.takeOrder(xcxOrderMain));
    }

    //接方 - 取消订单
    @PostMapping("/closeOrder")
    public AjaxResult backOrder(@RequestBody XcxOrderMain xcxOrderMain, @AuthenticationPrincipal UserDetails user)
    {
        return AjaxResult.updateData(xcxOrderMainService.closeOrder(xcxOrderMain));
    }

    //接方 - 同意发方撤销订单
    @PostMapping("/successBack")
    public AjaxResult successBack(@RequestBody XcxOrderMain xcxOrderMain, @AuthenticationPrincipal UserDetails user)
    {
        return AjaxResult.updateData(xcxOrderMainService.successBack(xcxOrderMain));
    }

    //接方 - 拒绝发方撤销订单
    @PostMapping("/noBack")
    public AjaxResult noBack(@RequestBody XcxOrderMain xcxOrderMain, @AuthenticationPrincipal UserDetails user)
    {
        return AjaxResult.updateData(xcxOrderMainService.noBack(xcxOrderMain));
    }

    //接方 - 取消撤销订单
    @PostMapping("/canceBackOrder")
    public AjaxResult canceBackOrder(@RequestBody XcxOrderMain xcxOrderMain, @AuthenticationPrincipal UserDetails user)
    {
        return AjaxResult.updateData(xcxOrderMainService.canceBackOrder(xcxOrderMain));
    }


    /* -------------------------------------- 分割线 上面发方 下面接方 ------------------------------------------------- */

    //发方 - 取消订单 - 平台扣除佣金
    @PostMapping("/cancelOrder")
    public AjaxResult cancelOrder(@RequestBody XcxOrderMain xcxOrderMain, @AuthenticationPrincipal UserDetails user)
    {
        return AjaxResult.updateData(xcxOrderMainService.cancelOrder(xcxOrderMain,raid));
    }


    //发方 - 已被接单 - 取消订单
    @PostMapping("/sendBackOrder")
    public AjaxResult sendBackOrder(@RequestBody XcxOrderMain xcxOrderMain, @AuthenticationPrincipal UserDetails user)
    {
        return AjaxResult.updateData(xcxOrderMainService.sendBackOrder(xcxOrderMain));
    }

    //发方 - 修改订单金额
    @PostMapping("/updateMoney")
    public AjaxResult updateMoney(@RequestBody XcxOrderMain xcxOrderMain, @AuthenticationPrincipal UserDetails user)
    {
        return AjaxResult.updateData(xcxOrderMainService.updateMoney(xcxOrderMain));
    }

    //发方 - 取消撤销订单
    @PostMapping("/sendCanceBack")
    public AjaxResult sendCanceBack(@RequestBody XcxOrderMain xcxOrderMain, @AuthenticationPrincipal UserDetails user)
    {
        return AjaxResult.updateData(xcxOrderMainService.sendCanceBack(xcxOrderMain));
    }

    //接方 - 同意发方撤销订单
    @PostMapping("/successSendBack")
    public AjaxResult successSendBack(@RequestBody XcxOrderMain xcxOrderMain, @AuthenticationPrincipal UserDetails user)
    {
        return AjaxResult.updateData(xcxOrderMainService.successSendBack(xcxOrderMain));
    }

    //接方 - 拒绝发方撤销订单
    @PostMapping("/noSendBack")
    public AjaxResult noSendBack(@RequestBody XcxOrderMain xcxOrderMain, @AuthenticationPrincipal UserDetails user)
    {
        return AjaxResult.updateData(xcxOrderMainService.noSendBack(xcxOrderMain));
    }


    //发方 - 完成订单
    @PostMapping("/confSuccess")
    public AjaxResult confSuccess(@RequestBody XcxOrderMain xcxOrderMain, @AuthenticationPrincipal UserDetails user)
    {
        return AjaxResult.updateData(xcxOrderMainService.confSuccess(xcxOrderMain,raid));
    }


    //接方 - 完成订单
    @PostMapping("/successOrder")
    public AjaxResult successOrder(@RequestBody XcxOrderMain xcxOrderMain, @AuthenticationPrincipal UserDetails user)
    {
        return AjaxResult.updateData(xcxOrderMainService.successOrder(xcxOrderMain));
    }


    //接方传接单凭证
    @PostMapping("/uploadTakeImg")
    public AjaxResult uploadTakeImg(@RequestBody XcxOrderMain xcxOrderMain, @AuthenticationPrincipal UserDetails user)
    {
        LoginUser loginUser =  (LoginUser)user;
        SysUser sysUser = loginUser.getUser();
        xcxOrderMain.setTakeUser(sysUser.getUserId()+"");

        return AjaxResult.updateData(xcxOrderMainService.uploadTakeImg(xcxOrderMain));
    }

    /* -------------------------------------- 分割线 上面接方 下面管理员 ------------------------------------------------- */

    //管理员-删除订单
    @PostMapping("/deleteOrder")
    public AjaxResult deleteOrder(@RequestBody XcxOrderMain xcxOrderMain, @AuthenticationPrincipal UserDetails user)
    {
        return AjaxResult.updateData(xcxOrderMainService.deleteOrder(xcxOrderMain));
    }

    //管理员-仲裁订单
    @PostMapping("/arbitrationOrder")
    public AjaxResult arbitrationOrder(@RequestBody XcxOrderMain xcxOrderMain, @AuthenticationPrincipal UserDetails user)
    {
        return AjaxResult.updateData(xcxOrderMainService.arbitrationOrder(xcxOrderMain));
    }




    /* -------------------------------------- 分割线 商品订单 用户操作 ------------------------------------------------- */
    //用户-未收货-取消订单
    @PostMapping("/goods/deleteOrder")
    public AjaxResult deleteGoodsOrder(@RequestBody XcxGoodsOrder xcxGoodsOrder, @AuthenticationPrincipal UserDetails user)
    {
        return AjaxResult.updateData(xcxOrderMainService.deleteGoodsOrder(xcxGoodsOrder));
    }

    //用户-确认收货
    @PostMapping("/goods/successGoods")
    public AjaxResult successGoods(@RequestBody XcxGoodsOrder xcxGoodsOrder, @AuthenticationPrincipal UserDetails user)
    {
        return AjaxResult.updateData(xcxOrderMainService.successGoods(xcxGoodsOrder));
    }

    //用户-申请退款
    @PostMapping("/goods/backGoods")
    public AjaxResult backGoods(@RequestBody XcxGoodsOrder xcxGoodsOrder, @AuthenticationPrincipal UserDetails user)
    {
        return AjaxResult.updateData(xcxOrderMainService.backGoods(xcxGoodsOrder));
    }

    //用户-完成订单
    @PostMapping("/goods/confSuccess")
    public AjaxResult confSuccess(@RequestBody XcxGoodsOrder xcxGoodsOrder, @AuthenticationPrincipal UserDetails user)
    {
        return AjaxResult.updateData(xcxOrderMainService.confSuccess(xcxGoodsOrder));
    }


    /* -------------------------------------- 分割线 商品订单 管理员操作 ------------------------------------------------- */

    //管理员-同意退款
    @PostMapping("/goods/successBackGoods")
    public AjaxResult successBackGoods(@RequestBody XcxGoodsOrder xcxGoodsOrder, @AuthenticationPrincipal UserDetails user)
    {
        return AjaxResult.updateData(xcxOrderMainService.successBackGoods(xcxGoodsOrder));
    }

    //管理员-拒绝退款
    @PostMapping("/goods/noBackGoods")
    public AjaxResult noBackGoods(@RequestBody XcxGoodsOrder xcxGoodsOrder, @AuthenticationPrincipal UserDetails user)
    {
        return AjaxResult.updateData(xcxOrderMainService.noBackGoods(xcxGoodsOrder));
    }
}
